package com.symaster.common.geom;

import com.symaster.common.geom.entity.Size;
import org.opencv.core.CvType;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * @author yinmiao
 * @date 2022-03-29 09:02
 */
public class PolygonPanel {

    public static final int CV_8UC3 = CvType.CV_8UC3;
    public static final int CV_8UC4 = CvType.CV_8UC4;
    private int cvType = CV_8UC3;
    private double w = 0;
    private double h = 0;
    private List<PolygonPanelItem> polygonPanelItemList;
    private Map<String, PolygonPanelItem> map;

    public PolygonPanel() {
        init();
    }

    public int getElementCount() {
        return polygonPanelItemList.size();
    }

    public int getCvType() {
        return cvType;
    }

    public void setCvType(int cvType) {
        this.cvType = cvType;
    }

    public void put(PolygonPanelItem item) {

        if (item.getSrcPolygon() != null && item.getSrcImagePath() != null) {
            w = Math.max(w, item.getX() + item.getWidth());
            h = Math.max(h, item.getY() + item.getHeight());
            polygonPanelItemList.add(item);
            map.put(item.getId(), item);
        }

        List<PolygonPanelItem> polygonPanelItemList1 = item.getPolygonPanelItems();
        if (polygonPanelItemList1 != null && polygonPanelItemList1.size() > 0) {
            for (PolygonPanelItem polygonPanelItem : polygonPanelItemList1) {
                put(polygonPanelItem);
            }
        }
    }

    private void init() {
        if (polygonPanelItemList == null) {
            polygonPanelItemList = new LinkedList<>();
        }
        if (map == null) {
            map = new HashMap<>();
        }
    }

    public List<PolygonPanelItem> list() {
        return polygonPanelItemList;
    }

    public PolygonPanelItem get(String id) {
        return map.get(id);
    }

    public Size getSize() {
        return new Size(w, h);
    }

    public List<PolygonPanelItem> getPolygonPanelItemList() {
        return polygonPanelItemList;
    }
}
